package com.teng.web.servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.teng.dao.UserDao;
import com.teng.dto.JSONResponse;

import com.teng.exception.OAException;

import com.teng.util.JSONUtil;
import com.teng.util.SystemUtil;
@WebServlet("/api/system/statistics")
public class StatisticsServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JSONResponse result=new JSONResponse();
		try {
			UserDao ud=new UserDao();
			int onlineUsers=LoginServlet.ON_LINE_USER;
			int totalAccount=ud.totalaccount();
			
			Map<String, Object> a=new HashMap<>();
			a.put("totalAccount", totalAccount);
			a.put("onlineUsers", onlineUsers);
			a.put("memoryPayload", (long)SystemUtil.getMemery());
			a.put("cpuPayload", SystemUtil.getCpuRatioForWindows());
			result.setCode("ok");
			result.setData(a);
		}catch(OAException e) {
			result.setCode("0001");
			result.setData(e.getMessage());
		}catch(Exception e) {
			result.setCode("error");
			result.setData("系统错误！请稍后再试");
			e.printStackTrace();
		}finally {
			resp.setCharacterEncoding("utf-8");
			resp.setContentType("application/json");
			Writer out=resp.getWriter();
			out.write(JSONUtil.toJsonString(result));
		}
	}
}
